<!DOCTYPE html>
<html>
<head>
	<title>B-M算法设计</title>
	<link href="bootstrap.min.css" rel="stylesheet">
	<script src="jquery-2.1.1.min.js"></script>
	<script src="bootstrap.min.js"></script>

</head>
<body>

	<script>
	$(document).ready(function(){

		$("#btnClick").click(function(){

			$("tbody").text("");
			byteString = $("input").val();
			byteLength = byteString.length;
			var pattern = /^[01]+$/;
			if(!byteString.match(pattern)){
				$("#btnClick").after("<h4 class = 'text-danger'>"+"您所输入的内容不是比特序列！请重新输入！"+"</h4>");
				$("input").val("");


				return;
			}
			
			$("input").val("");
			var nArray = new Array();
			var dnArray = new Array();
			var fnArray = new Array();
			fnArray[0] = ["10"];
			var lnArray = new Array();
			lnArray[0] = 0;
			var mArray = new Array();
			var fmArray = new Array();

			var aArray  = byteString.split("");
			var cArray =  byteString.split("").reverse();

			var tBodyString = new String("<tbody>");

				//calculate
				var j = 0;
				var k = 0;



				for(var i = 0;i<byteLength;i++){



					nArray[i] = i;
					//求dn[i]

					dnArray[i]=0;

					for(var j=0;j<fnArray[i].length;j++){

						if(fnArray[i][j][0]=="1"&&fnArray[i][j][1]!="0"){
							var add = parseInt(fnArray[i][j][1]);
							dnArray[i]+=parseInt(aArray[i-add]);
						}
					}
					dnArray[i] = dnArray[i]%2;

					if(dnArray[i]==aArray[i]) dnArray[i] = 0;
					else dnArray[i] = 1;


					if(dnArray[i]==0){
						fnArray[i+1] = fnArray[i].slice(0);
						lnArray[i+1] = lnArray[i];
						mArray[i] = " ";
					}else{
						if(lnArray[i]==0){
							//只走一次
							fnArray[i+1] =["10","1"+(i+1)];
							lnArray[i+1] = i+1;
							mArray[i] = " ";
						}else{

							var m = i;
							while(lnArray[m]==lnArray[m-1]){
								m--;
							}
							m = m-1;
							mArray[i] = m;
							fmArray[i] = fnArray[m].slice(0);
							var tempArray_1 = fnArray[i].slice(0);
							var tempArray_2 = fnArray[m].slice(0);

							 //乘x^(n-m)
							 for(var p = 0;p<tempArray_2.length;p++){
							 	if(tempArray_2[p][0]=="1" ){
							 		var add = parseInt(tempArray_2[p][1]);
							 		tempArray_2[p] = "1"+(add+i-m);
							 	}
							 }

							 var flag = 0;

							//多项式相加
							for(j=0;j<tempArray_2.length;j++){
								flag = 1;

								for(k = 0;k < tempArray_1.length;k++){

									if(tempArray_2[j][0]==tempArray_1[k][0]&&tempArray_1[k][1]==tempArray_2[j][1]){
										tempArray_1[k] = "00";
										flag = 0;
										break;
									}
								}

								if(flag){

									tempArray_1.splice(0,0,tempArray_2[j]);
								}
							}
							fnArray[i+1] = tempArray_1.slice(0);
							lnArray[i+1] = lnArray[i]>i+1-lnArray[i]?lnArray[i]:i+1-lnArray[i];
						}

					}

				}
				nArray[i] = i;
				dnArray[i] = " ";
				mArray[i] = " ";

				$("#btnClick").after("<h4 class = 'text-success'>求得的线性综合解为：</h4>"+"<p>&lt;"+ anaArray(fnArray[i])+","+lnArray[i]+"&gt;</p>");
				$("#btnClick").after("<h4 class = 'text-primary'>您所输入的比特序列为：</h4>"+"<p>"+"a<sup>("+byteLength+")</sup> = "+byteString+"</p>");



				//update the table
				for(var i = 0;i<=byteLength;i++){
					tBodyString+="<tr><td>"+nArray[i]+"</td>";
					tBodyString+="<td>"+dnArray[i]+"</td>";

					tBodyString+="<td>"+anaArray(fnArray[i])+"</td>";

					tBodyString+="<td>"+lnArray[i]+"</td>";
					tBodyString+="<td>"+mArray[i]+"</td>";
					tBodyString+="<td>"+anaArray(fmArray[i])+"</td>";

				}
				tBodyString+="</tr>";
				$("thead").after(tBodyString);
			});
});
</script>
<script>

	
	function anaArray(array){
		if(array==null) return " ";
		var string="1";
		array.sort();
		for(var i = 0 ;i<array.length;i++){
			if(array[i][0]==1&&array[i][1]!=0){

				string+=("+x<sup>"+array[i][1]+"</sup>");
			}

		}
		return string;
	}
</script>

<div class="jumbotron ">
	<div class="container">
		<h2>欢迎使用B-M算法计算器!</h2>

		<input type="text" class = "form-control" placeholder = "请输入所要计算的比特序列">
		<button type="button" id = "btnClick" class="btn btn-primary">确 定</button>



	</div>
</div>
<div class = "container">
	<table class="table table-bordered">
		<h3 class="text-center text-primary">计算结果</h3>
		<thead>
			<tr>
				<th>n</th>
				<th>d<sub>n</sub></th>
				<th>f<sub>n</sub></th>
				<th>l<sub>n</sub></th>
				<th>m</th>
				<th>f<sub>m</sub></th>
			</tr>
		</thead>
	</table>
	<p class = "text-right text-primary">-----By 20135113 xiaoYu-----</p>
</div>


</body>
</html>
